package concurrent;

import java.util.concurrent.*;

/**
 * @ClassName PhaserMockCountDownLatch
 * @Description PhaserMockCountDownLatch
 * @Author wanghaiwei
 * @Date 2022/2/22 11:20
 */
public class PhaserMockCyclicBarrier {
    public static ThreadPoolExecutor executorService = new ThreadPoolExecutor(10, 10,
            0L, TimeUnit.MILLISECONDS,
            new LinkedBlockingQueue<Runnable>());

    public static class ThreadPrint implements Runnable {
        private final Phaser ph;

        public ThreadPrint(Phaser ph) {
            this.ph = ph;
        }

        @Override
        public void run() {
            System.out.println("线程" + Thread.currentThread().getName() + "已完成步骤一");
            ph.arriveAndAwaitAdvance();
            System.out.println("线程" + Thread.currentThread().getName() + "已完成步骤二");
        }
    }

    public static void main(String[] args) {
        int threadLimit = 10;
        Phaser ph = new Phaser(threadLimit);
        for (int i = 0; i < threadLimit; i++) {
            executorService.submit(new ThreadPrint(ph));
        }
        executorService.shutdown();
    }
}
